<?php

/**
 * 公用数据模型
 */
if ( !defined( 'BASEPATH' ) ) exit( 'No direct script access allowed' );

class Common_model extends MY_Model
{

    public $_table = array(
        'common' => 'common',
    );
    public $_fields = array(
        'common' => array(
            'id' => 'id',
            'name' => 'name',
            'type' => 'type',
        ),
    );

    public function addZone( $zoneName )
    {
        $insertData = array(
            'type' => 'zone',
            'name' => $zoneName,
        );
        $this->db->insert( $this->_table['common'], $insertData );
        return $this->insert_id();
    }
    
    public function addRoom( $roomName )
    {
        $insertData = array(
            'type' => 'room',
            'name' => $roomName,
        );
        $this->db->insert( $this->_table['common'], $insertData );
        return $this->insert_id();
    }
    
    /**
     * 获取区域数据
     * @return arrray
     */
    public function getZoneData()
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'zone' )
                        ->get()
                        ->result_array();
    }

    /**
     * 根据区域ID获取区域数据
     * @param int $zoneId
     * @return arrray
     */
    public function getZoneDataById( $zoneId )
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'zone' )
                        ->where( 'id', $zoneId )
                        ->get()
                        ->row_array();
    }

    public function updateZoneNameById( $zoneName, $zoneId )
    {
        $this->db->where('id', $zoneId)->update($this->_table['common'], array('name' => $zoneName));
    }
    
    public function updateRoomNameById( $roomName, $roomId )
    {
        $this->db->where('id', $roomId)->update($this->_table['common'], array('name' => $roomName));
    }
    
    public function getZoneDataByName( $zoneName )
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'zone' )
                        ->where( 'name', $zoneName )
                        ->get()
                        ->row_array();
    }
    
    /**
     * 获取厅室数据
     * @return array
     */
    public function getRoomData()
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'room' )
                        ->get()
                        ->result_array();
    }

    /**
     * 根据厅室ID获取厅室数据
     * @param int $roomId
     * @return array
     */
    public function getRoomDataById( $roomId )
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'room' )
                        ->where( 'id', $roomId )
                        ->get()
                        ->row_array();
    }

    public function getRoomDataByName( $roomName )
    {
        return $this->db->select( 'id,name' )
                        ->from( $this->_table['common'] )
                        ->where( 'type', 'room' )
                        ->where( 'name', $roomName )
                        ->get()
                        ->row_array();
    }
    
}

?>
